package com.shop.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shop.dao.OrderDao;
import com.shop.entity.Order;
import com.shop.entity.User;

@Repository
@Transactional
public class OrderDaoImpl extends HibernateTemplate implements OrderDao{

	@Autowired
	private SessionFactory sessionFactory;
	@Override
	public List<Order> selectOrderByStatus(Order order) {
//		String hql = "from Order where eo_status = ? ";
		String hql = "from Order where eo_status = :eostatus ";
		Session session = sessionFactory.openSession();
		Query q = session.createQuery(hql);
		q.setInteger("eostatus", order.getEoStatus());
		List<Order> orderlist = q.list();
		return orderlist;
	}

	@Override
	public List<Order> findAllOrder() {
		return (List<Order>) super.find("from Order");
	}

	@Override
	public Serializable addOrder(Order order) {		
		return super.save(order);
	}

	@Override
	public Serializable deleteOrder(Order order) {
		super.delete(order);
		return null;
	}

	@Override
	public Serializable updateOrder(Order order) {
		super.update(order);
		return null;
	}

	@Override
	public Order selectByOrderId(Order order) {
		String hql = "from Order where eo_id = :eoid ";
		Session session = sessionFactory.openSession();
		Query q = session.createQuery(hql);
		q.setInteger("eoid", order.getEoId());
		return (Order) q.uniqueResult();
	}
	
	@Override
	public List<Order> selectOrderByUserId(User user) {
		String hql = "from Order where eo_user_id = :eouserid ";
		Session session = sessionFactory.openSession();
		Query q = session.createQuery(hql);
		q.setInteger("eouserid", user.getUserId());
		return q.list();
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	
	

}
